//
//  SettingViewController.m
//  MoneyLog
//
//  Created by Max on 11-6-20.
//  Copyright 2011年 wicresoft. All rights reserved.
//

#import "SettingViewController.h"
#import "AccountManageViewController.h"
#import "KindListViewController.h"

extern NSString *kSectionTitleKey;
extern NSString *kRowItemKey;

@implementation SettingViewController
@synthesize dataSourceArray;
@synthesize _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [dataSourceArray release];
    [_tableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"设置";
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.toolbarHidden = YES;
    self.dataSourceArray = [NSArray arrayWithObjects:
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             [self titleForSection:0], kSectionTitleKey,
                             [self rowArrayForSection:0], kRowItemKey, 
							 nil],
							
							[NSDictionary dictionaryWithObjectsAndKeys:
                             [self titleForSection:1], kSectionTitleKey,
                             [self rowArrayForSection:1], kRowItemKey,
							 nil],
							
							[NSDictionary dictionaryWithObjectsAndKeys:
                             [self titleForSection:2], kSectionTitleKey,
                             [self rowArrayForSection:2], kRowItemKey,
							 nil],							
							nil];
    [self._tableView reloadData];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - * Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[self.dataSourceArray objectAtIndex:section] objectForKey:kSectionTitleKey];
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSString *title = nil;
	
	return title;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return [self.dataSourceArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSArray *dataArray = [[self.dataSourceArray objectAtIndex:section] objectForKey:kRowItemKey];
  
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSArray *dataArray = [[self.dataSourceArray objectAtIndex:indexPath.section] objectForKey:kRowItemKey];
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
        
    return cell;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
    
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSLog(@"section: %d, row: %d", indexPath.section, indexPath.row);
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSLog(@"section: %d, row: %d", indexPath.section, indexPath.row);
}

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - * Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    AccountManageViewController *controller =[[AccountManageViewController alloc] init];
                    controller.displayType = 1;
                    [self.navigationController pushViewController:controller animated:YES];
                    [controller release];
                    break;
                }
                case 1:
                {
                    KindListViewController *controller = [[KindListViewController alloc] init];
                    [self.navigationController pushViewController:controller animated:YES];
                    [controller release];
                
                    break;
                }
                default:
                    break;
            }
            
            break;
        }
        default:
            break;
    }
    
}

#pragma mark - * Actions

-(NSString *)titleForSection:(NSInteger)section{
    NSString *title=@"";
    
    switch (section) {
        case 0:
            title = @"系统设置";
            break;
        case 1:
            title = @"数据管理";
            break;
        case 2:
            title = @"其他";
            break;
        default:
            break;
    }
    
    return title;
}

-(NSArray *)rowArrayForSection:(NSInteger)section {
    NSMutableArray *dataArray = nil;
    
    switch (section) {
        case 0:
            dataArray =[NSMutableArray arrayWithObjects:
                        @"帐户设置",
                        @"收支类别",nil];
            break;
        case 1:
            dataArray =[NSMutableArray arrayWithObjects:
                        @"数据备份",nil];
            break;
        case 2:
            dataArray =[NSMutableArray arrayWithObjects:
                        @"关于",
                        @"意见反馈",nil];
            break;
        default:
            break;
    }
    
    return dataArray;
}

@end
